/*
 * @Author: szx
 * @Date: 2022-03-16 16:51:35
 * @LastEditTime: 2022-03-16 17:16:09
 * @Description:
 * @FilePath: \learn\手撸JS源码\flat.js
 */
//默认不传参一层
Array.prototype.myFlat = function (deep = 1) {
    if (deep > 0) {
        const arr = this.reduce((preVal, curVal) => {
            if (Array.isArray(curVal)) {
                return [...preVal, ...curVal.myFlat(deep - 1)];
            }
            return [...preVal, curVal];
        }, []);
        return arr;
    } else {
        return this;
    }
};

const arr = [1, [2, [3, [5]]], 4];
let res = arr.myFlat(3);
console.log(res);
